package com.qf.person.dao.impl;

import com.qf.person.advanced.impl.PersonRowMapper;
import com.qf.person.dao.PersonDao;
import com.qf.person.entity.Person;
import com.qf.person.utils.DaoUtils;
import com.qf.person.utils.DateUtils;
import com.qf.person.utils.Dbutils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author wangyu
 * @Classname: PersonDaoImpl
 * @Description: TEST
 * @date 2022-04-07 16:36
 */
public class PersonDaoImpl implements PersonDao {
    private DaoUtils<Person> daoUtils = new DaoUtils();

    @Override
    public int insert(Person person) {
        String sql = "insert into person(name,bornDate,email,adress,age) values(?,?,?,?,?);";
        Object[] args = {person.getName(), DateUtils.utiToSql(person.getBornDate()), person.getEmail(), person.getAdress(), person.getAge()};
        return daoUtils.commonsUpdate(sql, args);
    }

    @Override
    public int update(Person person) {
        String sql = "update person set name=?,age=?,bornDate=?,email=?,adress=? where id=?";
        Object[] args = {person.getName(), person.getAge(), DateUtils.utiToSql(person.getBornDate()), person.getEmail(), person.getAdress(), person.getId()};
        return daoUtils.commonsUpdate(sql, args);
    }

    @Override
    public int delete(int id) {
        String sql = "delete from person where id = ?;";
        return daoUtils.commonsUpdate(sql, id);
    }

    @Override
    public Person select(int id) {
        String sql = "select * from person where id =?;";
        List<Person> list = daoUtils.commonsSelect(sql, new PersonRowMapper(), id);
        if(!list.isEmpty()){
           return list.get(0);
        }
        return null;
    }

    @Override
    public List<Person> selectAll() {
        String sql = "select * from person;";
        List<Person> list = daoUtils.commonsSelect(sql, new PersonRowMapper(),null);
        return list;
    }
}
